Contents | Index | < Browse | Browse >

LETTERbsearchULETTER Performs a binary search in arrays.

Overview
#include <stdlib.h>

r = bsearch(obj, array, members, size, compare);

void *r; // object found
const void *obj; // object searched for
const void *array; // pointer to array to search
size_t members; // number of members in array
size_t size; // size of each element
int (*compare)(const void *, const void *))
// pointer to comparison function

Portability
ANSI

Description
"bsearch" scans the array "array[0] ... array[members-1]" whose elements have the size "size" using the specified comparison function for an element equal to "*obj". The binary search performed requires the array to be sorted (eg. using "qsort").

The comparison function "compare" is subject to return the following values:
· a negative integer, if the first of the two objects is smaller than the second.
· a positive integer, if the first of the two objects is greater than the second.
· 0 is both objects are equal.

Returns
This function returns a pointer to the member which is equal to the element searched for. If no appropiate element is found NULL will be returned.

See also
qsort , strcmp